package com.corgi.mybatis.base.json;

import com.corgi.mybatis.util.AesUtil;
import com.corgi.mybatis.util.RsaUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @title: JacksonAesEncryptSerializer
 * @description:
 * @author: dengmiao
 * @create: 2019-10-29 11:53
 **/
public class JacksonBinaryAesEncryptSerializer extends JsonSerializer<byte[]> {

    private final static String AES_KEY = "1234567890123456";

    private final static String AES_IV = "1234567890000000";

    @Override
    public void serialize(byte[] bytes, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        String origin = new String(bytes, "utf-8");
        origin = RsaUtil.byte2Base64(bytes);
        // RsaUtil.byte2Base64(AesCbc.decrypt(bytes, AES_KEY, AES_IV));
        // jsonGenerator.writeString(AesUtil.encryptCBC(origin, AES_KEY, AES_IV));
        jsonGenerator.writeString(AesUtil.encrypt(origin, AES_KEY, AES_IV));
    }
}
